<template>
  <div>
    <span :class="['content', { isCompleted: item.isCompleted }]">{{ item.content }}</span>
    <span>
      <el-tooltip placement="left" :content="item.isCompleted ? '并非完成~~' : '完成!'">
        <el-button type="success" :icon="item.isCompleted ? Back : Check" circle @click="changeState" />
      </el-tooltip>
      <el-tooltip content="删除" placement="right">
        <el-button type="danger" :icon="Delete" circle @click="removeTodo" />
      </el-tooltip>
    </span>
  </div>
</template>

<script setup>
import {
  Delete, Check, Back
} from '@element-plus/icons-vue'
import { useTodoStore } from '../store/todoStore.js' // 引入store
import { ElMessageBox, ElMessage } from 'element-plus'
const todoStore = useTodoStore() // 实例化store

const props = defineProps(['item', 'index'])

const changeState = () => {
  props.item.isCompleted = !props.item.isCompleted;
}

const removeTodo = (index) => {
  ElMessageBox.confirm('确定删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    todoStore.removeTodo(index)
    ElMessage.success({
      message: '删除成功',
      duration: 1500,
      offset: 100
    })
  }).catch(() => {
    ElMessage.info({
      message: '取消删除',
      duration: 1500,
      offset: 100
    })
  })
}
</script>


<style scoped>
div {
  box-sizing: border-box;
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  height: 40px;
  margin: 15px 0;
  padding: 0 10px;
  border-radius: 8px;
  border: 1px solid #ccc;

}

div:hover {
  background-color: #f4f4f4;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.content {
  color: #666
}

.content.isCompleted {
  text-decoration: line-through;
  color: #999;
}

.el-button {
  box-shadow: 0 2px 6px rgba(0, 0, 0, .1);
}

.el-button:hover {
  transform: scale(1.1);
  transition: all 0.2s ease;
}
</style>